package com.sky.controller.admin;

import com.sky.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

@RestController("adminShopController")
@RequestMapping("/admin/shop")
@Api(tags = "店铺状态管理")
@Slf4j
public class Shopcontroller {

    public static final String SHOP_STATUS = "SHOP_STATUS";
    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 设置店铺营业状态
     *
     * @param status
     * @return
     */
    @PutMapping("/{status}")
    @ApiOperation("设置店铺营业状态")
    public Result setStatus(@PathVariable Integer status) {
        log.info("设置店铺营业状态:{}", status == 1 ? "营业中" : "打烊中");
        //将数据保存到Redis中
        redisTemplate.opsForValue().set(SHOP_STATUS, status);
        log.info("店铺的营业状态为 " + redisTemplate.opsForValue().get(SHOP_STATUS).toString());
        return Result.success();
    }


    /**
     * 管理端查询营业状态
     */
    @GetMapping("/status")
    @ApiOperation("管理端查询营业状态")
    public Result getStatus() {
        log.info("查询店铺的营业状态");
        Integer status = (Integer) redisTemplate.opsForValue().get(SHOP_STATUS);
        log.info("店铺的营业状态为 " + status);
        if (status == null) {
            return Result.error("查询失败");
        }
        return Result.success(status);
    }


}
